﻿// ===============================================================================
// DotNet.Platform 开发框架 2016 版权所有
// ===============================================================================

using System;
using System.Web.Mvc;
using System.Linq;
using DotNet.Core.Entity;
using DotNet.Core.Service;
using DotNet.Extensions;
using DotNet.Helper;
using DotNet.Utility;
using DotNet.Web.Areas.AuthWeb.Controllers;
using System.Collections.Generic;
using DotNet.Web.App_Start;

namespace DotNet.Web.Areas.WXCWeb.Controllers
{
    public class ArticleController : AuthController
    {
        private ArticleService service = new ArticleService();

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Grid(string key, string categoryId, string status, string startDate, string endDate)
        {
            var list = service.GetPageList(PageInfo(), key, categoryId, status.ToBoolOrNull(), startDate.ToDateTimeOrNull(), endDate.ToDateTimeOrNull());
            return View(list);
        }

        public ActionResult Create()
        {
            MarkCreate();
            return EditCore(new Article
            {
                Id = StringHelper.Guid()
            });
        }

        public ActionResult Edit(string id)
        {
            var entity = service.Get(id);
            if (entity == null) return NotFound(id);
            return EditCore(entity);
        }

        private ActionResult EditCore(Article entity)
        {
            return View("Edit", entity);
        }

        [HttpPost]
        [ValidateInput(false)]
        [ValidateAntiForgeryToken]
        public ActionResult Save(Article entity)
        {
            if (Request.Files.Count>0)
            {
                var info = UploadHelper.Upload(Request.Files[0]);
                entity.PhotoUrl = info.Url;
            }
            var result = service.Save(entity, IsCreate);
            return Json(result);
        }

        [HttpPost]
        public ActionResult Delete(string id)
        {
            service.Delete(id.SplitToArray());
            return Json(BoolMessage.True);
        }

        [HttpPost]
        public ActionResult Send(string id)
        {
            service.SetStatus(id, true);
            return Json(BoolMessage.True);
        }

        public ActionResult Details(string id)
        {
            var entity = service.Get(id);
            if (entity == null) return NotFound(id);
            return View(entity);
        }

        public ActionResult Export(string key, string categoryId, string status, string startDate, string endDate)
        {
            return Export(service.GetList(key, categoryId, status.ToBoolOrNull(), startDate.ToDateTimeOrNull(), endDate.ToDateTimeOrNull()));
        }

        private ActionResult NotFound(string id)
        {
            return NotFound("获取 文章信息 错误", $"无法找到 Id = {id} 的文章信息");
        }

        [HttpGet]
        public ActionResult GetTreeJson(string keyword)
        {
            var data = ArticleCategoryService.Instance.GetList();
            if (!string.IsNullOrEmpty(keyword))
            {
                data = data.TreeWhere(t => t.Name.Contains(keyword), "Id", "ParentId");
            }
            var treeList = new List<TreeEntity>();
            foreach (var item in data)
            {
                TreeEntity tree = new TreeEntity();
                bool hasChildren = data.Count(t => t.ParentId == item.Id) == 0 ? false : true;
                tree.id = item.Id;
                tree.text = item.Name;
                tree.value = item.Id;
                tree.isexpand = true;
                tree.complete = true;
                tree.hasChildren = hasChildren;
                tree.parentId = item.ParentId;
                treeList.Add(tree);
            }
            return Content(treeList.TreeToJson());
        }
    }
}
